import {
  createRouter,
  createWebHashHistory,
  createWebHistory,
} from "vue-router";

const Home = () => import("../views/Home.vue");
const Listview = () => import("@/views/Listview.vue");
const SearchView = () => import("@/views/Search.vue");
const Login = () => import("@/views/Login.vue");
const Me = () => import("@/views/Me.vue");
const TopList = () => import("@/views/TopList.vue");
const TopListDetail = () => import("@/views/TopListDetail.vue");
const RecommendSongs = () => import("@/views/recommendSongs.vue");
const PlayListDetail = () => import("@/views/playListDetail.vue");

const routes = [
  {
    path: "/",
    redirect: "/home",
  },
  { path: "/home", component: Home },
  { path: "/listview", component: Listview },
  { path: "/searchview", component: SearchView },
  { path: "/login", component: Login },
  { path: "/me", component: Me },
  { path: "/toplist", component: TopList },
  { path: "/toplistdetail", component: TopListDetail },
  { path: "/recommendsongs", component: RecommendSongs },
  { path: "/playlistdetail", component: PlayListDetail },
];

// 3. 创建路由实例
const router = createRouter({
  history: createWebHashHistory(),
  routes, // short for `routes: routes`
});
//设置路由拦截
router.beforeEach((to, from, next) => {
  if (to.path === "/me") {
    localStorage.getItem("cookie") ? next() : next("/login");
  }
  next();
});
export default router;
